<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 推广奖励定时任务
 */
class SpreadAward extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('plat_award_list_model');
        $this->load->model('users_model');
        $this->load->model('account_model');
        $this->load->model('finance_list_model');
        $this->load->model('experience_list_model');
    }
    function index() {
        $where = array();
        $where['is_raward_father'] = 0;
        $where['is_receive'] = 1;
        //获取未返利给父级的记录
        $list = $this->plat_award_list_model->get_raward_no_list($where,0,15);
        if($list){
            foreach ($list as $val){
                $user_id = $val['user_id'];//用户id
                $user = $this->users_model->get_user_by_uid($user_id);
                $username = name_formart($user['username']);
                $award = $val['award'];
                $exper = $val['exper'];
                $parents = $this->users_model->get_5_parents($user_id);//获取五级父类的用户id
                if($parents){//有父类
                    $this->plat_award_list_model->update(array('is_raward_father'=>1),$val['id']);
                    
                    if($parents[0]){//一级父类
                        $this_time = time();
                        $award_temp = $award*0.2;//20%推广返利溜溜币
                        $exper_temp = intval($exper*0.2);
                        $parent_id = intval($parents[0]);
                        //获取账号信息
                        $account = $this->account_model->get($parent_id);
                        //更新账号信息
                        $account_data = array();
                        $account_data['total'] = $account['total'] + $award_temp;
                        $account_data['balance'] = $account['balance'] + $award_temp;
                        $account_data['experience'] = $account['experience'] + $exper_temp;
                        $account_data['experience_balance'] = $account['experience_balance'] + $exper_temp;
                        $this->account_model->update($account_data,$parent_id);
                        //奖励溜溜币
                        $finance_data = array();
                        $finance_data['user_id'] = $parent_id;
                        $finance_data['content'] = "一级好友试玩奖励".$award_temp."溜溜币";
                        $finance_data['finance'] = $award_temp;
                        $finance_data['finance_balance'] = $account_data['balance'];
                        $finance_data['reward_type'] = 'spread_award';
                        $finance_data['type'] = 1;
                        $finance_data['create_time'] = $this_time;
                        $finance_data['month'] = date("Ym");
                        $finance_data['day'] = date("Ymd");
                        $finance_data['friend_id'] = $user_id;
                        $finance_data['friend_flag'] = 1;
                        $this->finance_list_model->insert($finance_data);
                        
                        $experience_data = array();
                        $experience_data['user_id'] = $parent_id;
                        $experience_data['content'] = "一级好友试玩奖励".$exper_temp."积分";
                        $experience_data['experience'] = $exper_temp;
                        $experience_data['experience_balance'] = $account_data['experience_balance'];
                        $experience_data['reward_type'] = 'spread_award';
                        $experience_data['type'] = 1;
                        $experience_data['create_time'] = $this_time;
                        $experience_data['month'] = date("Ym");
                        $experience_data['day'] = date("Ymd");
                        $experience_data['friend_id'] = $user_id;
                        $experience_data['friend_flag'] = 1;
                        $this->experience_list_model->insert($experience_data);
                        //推广奖励通知
                        $message = "您获得了由一级好友【".$username."】所带来的推广返利：".$award_temp."溜溜币和".$exper_temp."积分，请注意查收。感谢您对弹溜溜的支持！如有疑问请联系在线客服或拨打客服热线：400-088-0809。";
                        $this->db->insert("users_award_notice", array('user_id'=>$parent_id,'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    
                    }
                    
                    if($parents[1]){//一级父类
                        $this_time = time();
                        $award_temp = $award*0.1;//10%推广返利溜溜币
                        $exper_temp = intval($exper*0.1);
                        $parent_id = intval($parents[1]);
                        //获取账号信息
                        $account = $this->account_model->get($parent_id);
                        //更新账号信息
                        $account_data = array();
                        $account_data['total'] = $account['total'] + $award_temp;
                        $account_data['balance'] = $account['balance'] + $award_temp;
                        $account_data['experience'] = $account['experience'] + $exper_temp;
                        $account_data['experience_balance'] = $account['experience_balance'] + $exper_temp;
                        $this->account_model->update($account_data,$parent_id);
                        //奖励溜溜币
                        $finance_data = array();
                        $finance_data['user_id'] = $parent_id;
                        $finance_data['content'] = "二级好友试玩奖励".$award_temp."溜溜币";
                        $finance_data['finance'] = $award_temp;
                        $finance_data['finance_balance'] = $account_data['balance'];
                        $finance_data['reward_type'] = 'spread_award';
                        $finance_data['type'] = 1;
                        $finance_data['create_time'] = $this_time;
                        $finance_data['month'] = date("Ym");
                        $finance_data['day'] = date("Ymd");
                        $finance_data['friend_id'] = $user_id;
                        $finance_data['friend_flag'] = 2;
                        $this->finance_list_model->insert($finance_data);
                        
                        $experience_data = array();
                        $experience_data['user_id'] = $parent_id;
                        $experience_data['content'] = "二级好友试玩奖励".$exper_temp."积分";
                        $experience_data['experience'] = $exper_temp;
                        $experience_data['experience_balance'] = $account_data['experience_balance'];
                        $experience_data['reward_type'] = 'spread_award';
                        $experience_data['type'] = 1;
                        $experience_data['create_time'] = $this_time;
                        $experience_data['month'] = date("Ym");
                        $experience_data['day'] = date("Ymd");
                        $experience_data['friend_id'] = $user_id;
                        $experience_data['friend_flag'] = 2;
                        $this->experience_list_model->insert($experience_data);
                        //奖励通知
                        $message = "您获得了由二级好友【".$username."】所带来的推广返利：".$award_temp."溜溜币和".$exper_temp."积分，请注意查收。感谢您对弹溜溜的支持！如有疑问请联系在线客服或拨打客服热线：400-088-0809。";
                        $this->db->insert("users_award_notice", array('user_id'=>$parent_id,'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    
                    }
                    
                    if($parents[2]){//三级父类
                        $this_time = time();
                        $award_temp = $award*0.08;//8%推广返利溜溜币
                        $exper_temp = intval($exper*0.08);
                        $parent_id = intval($parents[2]);
                        //获取账号信息
                        $account = $this->account_model->get($parent_id);
                        //更新账号信息
                        $account_data = array();
                        $account_data['total'] = $account['total'] + $award_temp;
                        $account_data['balance'] = $account['balance'] + $award_temp;
                        $account_data['experience'] = $account['experience'] + $exper_temp;
                        $account_data['experience_balance'] = $account['experience_balance'] + $exper_temp;
                        $this->account_model->update($account_data,$parent_id);
                        //奖励溜溜币
                        $finance_data = array();
                        $finance_data['user_id'] = $parent_id;
                        $finance_data['content'] = "三级好友试玩奖励".$award_temp."溜溜币";
                        $finance_data['finance'] = $award_temp;
                        $finance_data['finance_balance'] = $account_data['balance'];
                        $finance_data['reward_type'] = 'spread_award';
                        $finance_data['type'] = 1;
                        $finance_data['create_time'] = $this_time;
                        $finance_data['month'] = date("Ym");
                        $finance_data['day'] = date("Ymd");
                        $finance_data['friend_id'] = $user_id;
                        $finance_data['friend_flag'] = 3;
                        $this->finance_list_model->insert($finance_data);
                        
                        $experience_data = array();
                        $experience_data['user_id'] = $parent_id;
                        $experience_data['content'] = "三级好友试玩奖励".$exper_temp."积分";
                        $experience_data['experience'] = $exper_temp;
                        $experience_data['experience_balance'] = $account_data['experience_balance'];
                        $experience_data['reward_type'] = 'spread_award';
                        $experience_data['type'] = 1;
                        $experience_data['create_time'] = $this_time;
                        $experience_data['month'] = date("Ym");
                        $experience_data['day'] = date("Ymd");
                        $experience_data['friend_id'] = $user_id;
                        $experience_data['friend_flag'] = 3;
                        $this->experience_list_model->insert($experience_data);
                        //奖励通知
                        $message = "您获得了由三级好友【".$username."】所带来的推广返利：".$award_temp."溜溜币和".$exper_temp."积分，请注意查收。感谢您对弹溜溜的支持！如有疑问请联系在线客服或拨打客服热线：400-088-0809。";
                        $this->db->insert("users_award_notice", array('user_id'=>$parent_id,'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    
                    }
                    
                    if($parents[3]){//四级父类
                        $this_time = time();
                        $award_temp = $award*0.07;//7%推广返利溜溜币
                        $exper_temp = intval($exper*0.07);
                        $parent_id = intval($parents[3]);
                        //获取账号信息
                        $account = $this->account_model->get($parent_id);
                        //更新账号信息
                        $account_data = array();
                        $account_data['total'] = $account['total'] + $award_temp;
                        $account_data['balance'] = $account['balance'] + $award_temp;
                        $account_data['experience'] = $account['experience'] + $exper_temp;
                        $account_data['experience_balance'] = $account['experience_balance'] + $exper_temp;
                        $this->account_model->update($account_data,$parent_id);
                        //奖励溜溜币
                        $finance_data = array();
                        $finance_data['user_id'] = $parent_id;
                        $finance_data['content'] = "四级好友试玩奖励".$award_temp."溜溜币";
                        $finance_data['finance'] = $award_temp;
                        $finance_data['finance_balance'] = $account_data['balance'];
                        $finance_data['reward_type'] = 'spread_award';
                        $finance_data['type'] = 1;
                        $finance_data['create_time'] = $this_time;
                        $finance_data['month'] = date("Ym");
                        $finance_data['day'] = date("Ymd");
                        $finance_data['friend_id'] = $user_id;
                        $finance_data['friend_flag'] = 4;
                        $this->finance_list_model->insert($finance_data);
                        
                        $experience_data = array();
                        $experience_data['user_id'] = $parent_id;
                        $experience_data['content'] = "四级好友试玩奖励".$exper_temp."积分";
                        $experience_data['experience'] = $exper_temp;
                        $experience_data['experience_balance'] = $account_data['experience_balance'];
                        $experience_data['reward_type'] = 'spread_award';
                        $experience_data['type'] = 1;
                        $experience_data['create_time'] = $this_time;
                        $experience_data['month'] = date("Ym");
                        $experience_data['day'] = date("Ymd");
                        $experience_data['friend_id'] = $user_id;
                        $experience_data['friend_flag'] = 4;
                        $this->experience_list_model->insert($experience_data);
                        //奖励通知
                        $message = "您获得了由四级好友【".$username."】所带来的推广返利：".$award_temp."溜溜币和".$exper_temp."积分，请注意查收。感谢您对弹溜溜的支持！如有疑问请联系在线客服或拨打客服热线：400-088-0809。";
                        $this->db->insert("users_award_notice", array('user_id'=>$parent_id,'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    }
                    
                    if($parents[4]){//五级父类
                        $this_time = time();
                        $award_temp = $award*0.05;//5%推广返利溜溜币
                        $exper_temp = intval($exper*0.05);
                        $parent_id = intval($parents[4]);
                        //获取账号信息
                        $account = $this->account_model->get($parent_id);
                        //更新账号信息
                        $account_data = array();
                        $account_data['total'] = $account['total'] + $award_temp;
                        $account_data['balance'] = $account['balance'] + $award_temp;
                        $account_data['experience'] = $account['experience'] + $exper_temp;
                        $account_data['experience_balance'] = $account['experience_balance'] + $exper_temp;
                        $this->account_model->update($account_data,$parent_id);
                        //奖励溜溜币
                        $finance_data = array();
                        $finance_data['user_id'] = $parent_id;
                        $finance_data['content'] = "五级好友试玩奖励".$award_temp."溜溜币";
                        $finance_data['finance'] = $award_temp;
                        $finance_data['finance_balance'] = $account_data['balance'];
                        $finance_data['reward_type'] = 'spread_award';
                        $finance_data['type'] = 1;
                        $finance_data['create_time'] = $this_time;
                        $finance_data['month'] = date("Ym");
                        $finance_data['day'] = date("Ymd");
                        $finance_data['friend_id'] = $user_id;
                        $finance_data['friend_flag'] = 5;
                        $this->finance_list_model->insert($finance_data);
                        
                        $experience_data = array();
                        $experience_data['user_id'] = $parent_id;
                        $experience_data['content'] = "五级好试玩奖励".$exper_temp."积分";
                        $experience_data['experience'] = $exper_temp;
                        $experience_data['experience_balance'] = $account_data['experience_balance'];
                        $experience_data['reward_type'] = 'spread_award';
                        $experience_data['type'] = 1;
                        $experience_data['create_time'] = $this_time;
                        $experience_data['month'] = date("Ym");
                        $experience_data['day'] = date("Ymd");
                        $experience_data['friend_id'] = $user_id;
                        $experience_data['friend_flag'] = 5;
                        $this->experience_list_model->insert($experience_data);
                        //奖励通知
                        $message = "您获得了由五级好友【".$username."】所带来的推广返利：".$award_temp."溜溜币和".$exper_temp."积分，请注意查收。感谢您对弹溜溜的支持！如有疑问请联系在线客服或拨打客服热线：400-088-0809。";
                        $this->db->insert("users_award_notice", array('user_id'=>$parent_id,'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    }
                    
                    echo "推广奖励完成".$val['id'];
                }else{//无父类
                    //更新数据
                    $this->plat_award_list_model->update(array('is_raward_father'=>1),$val['id']);
                }
            }
        }else{
            echo date('Y-m-d H:i:s')."无推广奖励记录！";
        }
    }
}
